package com.iwhalecloud.bss.kite.cucc.service.custview.evolve.task;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.google.common.collect.Lists;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfServiceBus;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CommonInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewAttr;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewBaseVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;

import com.ztesoft.bss.common.util.SpringUtil;

/**
 * @ClassName : CommonInfoTask
 * @Description : 公共信息
 * @Author : xie.zhengjun
 * @Date : 2021/4/30 20:04
 */
public class CommonInfoTask extends AbstractCustViewTask{
    @Override
    public CustViewBaseVO call(Object[] args) {
        if (args.length != 3) {
            return null;
        }
        Map<String, Object> userDetail = (Map<String, Object>) args[0];
        CustViewVO custViewVO = (CustViewVO) args[1];
        String ProvinceCode = (String) args[2];
        CommonInfoVO commonInfoVO = new CommonInfoVO();
        custViewVO.setCommon(commonInfoVO);
        if (KiteMapUtils.isNotEmpty(userDetail)) {
            // 联系人
            getLinkman(userDetail, commonInfoVO);
            // 发展人
            getDeveloper(userDetail, ProvinceCode, commonInfoVO);
            // 代理人
            getAgent(userDetail, commonInfoVO, ProvinceCode);
            // 客户经理
            getCustManager(userDetail, commonInfoVO);
        }
        return commonInfoVO;
    }

    private void getCustManager(Map<String, Object> userDetail, CommonInfoVO commonInfoVO) {
        List<Map<String, Object>> userItemInfos = (List<Map<String, Object>>) userDetail.get("USER_ITEM_INFOS");
        if (KiteListUtils.isNotEmpty(userItemInfos)) {
            String custManagerName = "";
            String custManagerPhone = "";
            String custManagerEmail = "";
            for (Map<String, Object> userItemInfo : userItemInfos) {
                if (KiteStringUtils.isEqual(KiteMapUtils.getString(userItemInfo, "CRM_ATTR_CODE"), LocalKeyConsts.CUST_MANAGER_NAME) || (KiteStringUtils.isEqual(KiteMapUtils.getString(userItemInfo, "CRM_ATTR_CODE"), LocalKeyConsts.CUST_MANAGER_NAME_NEW))) {
                    custManagerName = KiteMapUtils.getString(userItemInfo, "ATTR_VALUE");
                }
                if (KiteStringUtils.isEqual(KiteMapUtils.getString(userItemInfo, "CRM_ATTR_CODE"), LocalKeyConsts.CUST_MANAGER_PHONE) || KiteStringUtils.isEqual(KiteMapUtils.getString(userItemInfo, "CRM_ATTR_CODE"), LocalKeyConsts.CUST_MANAGER_PHONE_NEW)) {
                    custManagerPhone = KiteMapUtils.getString(userItemInfo, "ATTR_VALUE");
                }
                if (KiteStringUtils.isEqual(KiteMapUtils.getString(userItemInfo, "CRM_ATTR_CODE"), LocalKeyConsts.CUST_MANAGER_EMAIL) || KiteStringUtils.isEqual(KiteMapUtils.getString(userItemInfo, "CRM_ATTR_CODE"), LocalKeyConsts.CUST_MANAGER_EMAIL_NEW)) {
                    custManagerEmail = KiteMapUtils.getString(userItemInfo, "ATTR_VALUE");
                }
            }
            commonInfoVO.setCustManager(Lists
                .newArrayList(new CustViewAttr("客户经理姓名", custManagerName, custManagerName),
                    new CustViewAttr("客户经理联系电话", custManagerPhone, custManagerPhone),
                    new CustViewAttr("客户经理联系邮箱", custManagerEmail, custManagerEmail)));
        }
    }

    private void getAgent(Map<String, Object> userDetail, CommonInfoVO commonInfoVO, String provinceCode) {
        List<Map<String, Object>> userAgentInfos = (List<Map<String, Object>>) userDetail.get("USER_AGENT_INFOS");
        String agentChnlCode = "";
        String developStaffId = "";
        String developStaffName = "";
        String agentContact = "";
        if (KiteListUtils.isNotEmpty(userAgentInfos)) {
            Map<String, Object> userDevelopInfo = userAgentInfos.get(0);
            // 代理商渠道ID
            agentChnlCode = KiteMapUtils.getString(userDevelopInfo, "AGENT_CHNL_CODE");
            // 代理商ID
            developStaffId = KiteMapUtils.getString(userDevelopInfo, "DEVELOP_STAFF_ID");
            // 代理商名称
            developStaffName = KiteMapUtils.getString(userDevelopInfo, "DEVELOP_STAFF_NAME");
            if (KiteStringUtils.isEmpty(developStaffName)) {
                IInfServiceBus infServiceBus = SpringUtil.getBean(IInfServiceBus.class);
                developStaffName = infServiceBus.queryDeveloChannelName(agentChnlCode, provinceCode);
            }
            // 代理商联系电话
            agentContact = KiteMapUtils.getString(userDevelopInfo, "AGENT_CONTACT");
        }
        commonInfoVO.setAgent(Lists.newArrayList(new CustViewAttr("代理商渠道ID", agentChnlCode, agentChnlCode),
            new CustViewAttr("代理商名称", developStaffName, developStaffName),
            new CustViewAttr("代理商联系电话", agentContact, agentContact)));
    }

    private void getDeveloper(Map<String, Object> userDetail, String provinceCode, CommonInfoVO commonInfoVO) {
        List<Map<String, Object>> userDevelopInfos = (List<Map<String, Object>>) userDetail.get("USER_DEVELOP_INFOS");
        if (KiteListUtils.isNotEmpty(userDevelopInfos)) {
            Map<String, Object> userDevelopInfo = userDevelopInfos.get(0);
            String developStaffName = KiteMapUtils.getString(userDevelopInfo, "DEVELOP_STAFF_NAME");
            String developDepartId = KiteMapUtils.getString(userDevelopInfo, "DEVELOP_DEPART_ID");
            String developStaffId = KiteMapUtils.getString(userDevelopInfo, "DEVELOP_STAFF_ID");
            String developContact = KiteMapUtils.getString(userDevelopInfo, "DEVELOP_CONTACT");
            IInfServiceBus infServiceBus = SpringUtil.getBean(IInfServiceBus.class);
            String channelName = infServiceBus.queryDeveloChannelName(developDepartId, provinceCode);
            channelName = Optional.ofNullable(channelName).orElse("");
            CustViewAttr developerChannelNamee = new CustViewAttr("发展人渠道名称", channelName, channelName);
            CustViewAttr developerName = new CustViewAttr("发展人名称", developStaffName, developStaffName);
            CustViewAttr developerChannelId = new CustViewAttr("发展人渠道id", developDepartId, developDepartId);
            CustViewAttr developerStaffId = new CustViewAttr("发展人员工id", developStaffId, developStaffId);
            CustViewAttr developerPhone = new CustViewAttr("发展人联系方式", developContact, developContact);
            // TODO: 这个字段暂时设为否
            CustViewAttr isSendMsg = new CustViewAttr("是否发送短信", "否", "否");
            commonInfoVO.setDeveloper(Lists
                .newArrayList(developerChannelNamee, developerName, developerChannelId, developerStaffId,
                    developerPhone, isSendMsg));
        }
    }

    private void getLinkman(Map<String, Object> userDetail, CommonInfoVO commonInfoVO) {
        List<Map<String, Object>> userContactInfos = (List<Map<String, Object>>) userDetail.get("USER_CONTACT_INFOS");
        if (KiteListUtils.isNotEmpty(userContactInfos)) {
            Map<String, Object> userContactInfo = userContactInfos.get(0);
            String contactName = KiteMapUtils.getString(userContactInfo, "CONTACT_NAME");
            String contactMobile = KiteMapUtils.getString(userContactInfo, "CONTACT_MOBILE");
            String contactEmail = KiteMapUtils.getString(userContactInfo, "CONTACT_EMAIL");
            CustViewAttr linkmanName = new CustViewAttr("联系人名称", contactName, contactName);
            CustViewAttr linkmanPhone = new CustViewAttr("联系人电话", contactMobile, contactMobile);
            CustViewAttr linkmanEmail = new CustViewAttr("联系人邮箱", contactEmail, contactEmail);
            commonInfoVO.setLinkman(Lists.newArrayList(linkmanName, linkmanPhone, linkmanEmail));
        }
    }
}
